package look4ss.aplicativo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.HashMap;
import java.util.Map;

public class ActivityRegistro extends AppCompatActivity {
    private EditText enderecoEmaill, Senhaa, nome;
    private Button botaoRegistrar;
    private ProgressBar barraDeLoading;
    private TextView cancelar;
    private SharedPreferences sharp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registro);

        nome = findViewById(R.id.nome_usuario);
        enderecoEmaill = findViewById(R.id.endereco_de_email);
        Senhaa = findViewById(R.id.senha_login);
        botaoRegistrar = findViewById(R.id.registrar);
        botaoRegistrar.setTextColor(ContextCompat.getColor(ActivityRegistro.this, R.color.textoBotoes));
        cancelar = findViewById(R.id.cancelar);
        barraDeLoading = findViewById(R.id.barraLoading);
        final TextInputLayout txtNome = findViewById(R.id.layout_entrada_texto1);
        final TextInputLayout txtEmail = findViewById(R.id.layout_entrada_texto2);
        final TextInputLayout txtSenha = findViewById(R.id.layout_entrada_texto2);

        txtNome.setHintEnabled(true);
        txtEmail.setHintEnabled(true);
        txtSenha.setHintEnabled(true);

        cancelar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        botaoRegistrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = enderecoEmaill.getText().toString();
                String senha = Senhaa.getText().toString();

                sharp = getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor edtr = sharp.edit();
                edtr.putString("email", email);
                edtr.putString("senha", senha);
                edtr.apply();

                if (TextUtils.isEmpty(email)) {
                    txtEmail.setError(getString(R.string.digite_email));
                    return;
                } else {
                    txtEmail.setError(null);
                }
                if (TextUtils.isEmpty(senha)) {
                    txtSenha.setError(getString(R.string.digite_senha));
                    return;
                } else {
                    txtSenha.setError(null);
                }

                registrarUsuario();
                barraDeLoading.setVisibility(View.VISIBLE);
            }
        });
    }

    private void registrarUsuario(){
        StringRequest requisicao = new StringRequest(StringRequest.Method.POST, variaveisGlobais.urlApiRegistro, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if(response.contains("1")){
                    barraDeLoading.setVisibility(View.GONE);
                    startActivity(new Intent(ActivityRegistro.this, ActivityEnviar.class));
                }else{
                    barraDeLoading.setVisibility(View.GONE);
                    TastyToast.makeText(getApplicationContext(), "Erro ao conectar-se ao banco de dados!", Toast.LENGTH_SHORT, TastyToast.ERROR);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("registro", "true");
                params.put("email", enderecoEmaill.getText().toString().trim());
                params.put("nome", nome.getText().toString().trim());
                params.put("senha", Senhaa.getText().toString().trim());
                return params;
            }
        };
        Volley.newRequestQueue(this).add(requisicao);
    }
}